home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: String Arrays and string parsing
- Date: Tue, 20 Feb 96 20:01:40 GMT
- Organization: none
- Message-ID: <824846500snz@genesis.demon.co.uk>
- References: <31287278.789445@news.inforamp.net> <4gaqrj$3kn@hacgate2.hac.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4gaqrj$3kn@hacgate2.hac.com>
- collins@thor.tu.hac.com "Ron Collins" writes:
-
- >Danny Heuman (dsheuman@inforamp.net) wrote:
- >: I have day, month, and year as DDMMMYYYY and would like to parse it
- >: out in to DD, MMM, YYYY. I can parse the DD out by using
- >: strncpy(into1, from1, 2). I can parse the YYYY out by doing
- >: strcpy(into3, from1 + 5). How can I parse out the MMM? Can I use
- >: either of these functions that work above, strncpy or strcpy? If so,
- >: once parsed, do I have to attach an '\0' to the end of it to keep it
- >: as a character string?
- >
- >: Thanks,
- >
- >
- >: Danny Heuman
- >: dsheuman@inforamp.net
- >
- >You've almost got it with the "strncpy()". Try this:
- >(assuming you define "from1" somewhere)
- >
- >#include <string.h>
- >
- >....
- >
- >char into1[3];
- >char into2[4];
- >char into3[5];
- >
- >...
- >
- > strncpy(into1,from1,2);
- > into1[2] = 0;
- > strncpy(into2,from1+2,3);
- > into2[3] = 0;
- > strncpy(into3,from1+5,4);
- > into3[4] = 0;
-
- A possibly better approach in general (although it doesn't make much
- difference here) is:
-
- *into1 = '\0';
- strncat(into1,from1,2);
- *into2 = '\0';
- strncat(into2,from1+2,3);
- *into3 = '\0';
- strncat(into3,from1+5,4);
-
- Or use strcpy() for the last if you can guarantee that from1 is null
- terminated after YYYY. A more compact approach is
-
- sscanf(from1, "%2s%3s%4s", into1, into2, into3);
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-